package com.wwwarehouse.contract.convert_to_pc;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.bean.response.GetImportToolIndexBean;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.contract.bean.TestGetOpIdBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/9/18.
 */
@Route(path = "/contract/TestFragment")
public class TestFragment extends BaseFragment {

	String arr[] = new String[]{"商品" , "工具" , "服务"};

	@Override
	public void setTitle() {


	}

	@Override
	public void requestDatas() {

	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.test_app_fragment, container, false);
	}

	public String getRandomString(){
		String ran = null;
		int id = (int) (Math.random()*(arr.length-1));//随机产生一个index索引
		ran = arr[id];
		return ran;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		view.findViewById(R.id.have).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Map<String, Object> map = new HashMap<>();
				map.put("businessUnitId", 10002098);
				map.put("templateType", "工具");
				map.put("operationUkid", null);
				NoHttpUtils.httpPost(Constant.GET_IMPORT_TOOLS_INDEX, map, new NoHttpUtils.OnResponseListener() {
					@Override
					public void onSucceed(CommonClass commonClass, int i) {
						if (commonClass != null && commonClass.getData() != null) {
							GetImportToolIndexBean bean = JSON.parseObject(commonClass.getData().toString(), GetImportToolIndexBean.class);
							if (bean != null) {
								if (bean.getPageType() == 2) {
									//跳转到生成短链接的页面
									Bundle bundle = new Bundle();
									bundle.putSerializable(Constant.KEY_INTENT_IMPORT_TOOL, bean);
									ImportHaveSortFragment importHaveSortFragment = new ImportHaveSortFragment();
									importHaveSortFragment.setArguments(bundle);
									pushFragment(importHaveSortFragment);
								} else if (bean.getPageType() == 3) {
									//跳转到时间页面
									Bundle bundle = new Bundle();
									bundle.putSerializable(Constant.KEY_INTENT_IMPORT_TIME, bean);
									DurationTimeFragment durationTimeFragment = new DurationTimeFragment();
									durationTimeFragment.setArguments(bundle);
									pushFragment(durationTimeFragment);
								}
							}
						}
					}

					@Override
					public void onFailed(String s, int i) {

					}

					@Override
					public void onFinish(int i) {

					}
				} , 0);
			}
		});

		view.findViewById(R.id.no_have).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Map<String, Object> map1 = new HashMap<>();
				map1.put("businessUnitId", 10002098);
				map1.put("templateType", "");
				map1.put("operationUkid", null);
				NoHttpUtils.httpPost(ContractConstant.GET_IMPORT_TOOLS_INDEX, map1, new NoHttpUtils.OnResponseListener() {
					@Override
					public void onSucceed(CommonClass commonClass, int i) {
						if (commonClass != null && commonClass.getData() != null) {
							GetImportToolIndexBean bean = JSON.parseObject(commonClass.getData().toString(), GetImportToolIndexBean.class);
							if (bean != null) {
								if (bean.getPageType() == 1) {
									//跳转到生成短链接的页面
									Bundle bundle = new Bundle();
									bundle.putSerializable(ContractConstant.KEY_INTENT_IMPORT_TOOL_BEAN, bean);
									BulkImportFragment bulkImportFragment = new BulkImportFragment();
									bulkImportFragment.setArguments(bundle);
									pushFragment(bulkImportFragment);
								} else if (bean.getPageType() == 3) {
									//跳转到时间页面
									Bundle bundle = new Bundle();
									bundle.putSerializable(ContractConstant.KEY_INTENT_IMPORT_TOOL_BEAN, bean);
									DurationTimeFragment durationTimeFragment = new DurationTimeFragment();
									durationTimeFragment.setArguments(bundle);
									pushFragment(durationTimeFragment);
								}
							}
						}
					}

					@Override
					public void onFailed(String s, int i) {

					}

					@Override
					public void onFinish(int i) {

					}
				} , 0);
			}
		});


		//任务卡片进行中的
		view.findViewById(R.id.renwuing).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Map<String, Object> map = new HashMap<>();
				map.put("opUserId", sp.getValue(Constant.sp_User_Id));
				map.put("operationType", "IMPORT_EXCEL");
				map.put("ownerId",  sp.getValue(Constant.sp_User_Id));
				map.put("status", 5);

				Map<String, Object> map1 = new HashMap<>();
				map1.put("pageNo", 1);
				map1.put("pageSize", 100);
				map1.put("params", map);

				NoHttpUtils.httpPost("router/api?method=TsOperationService.queryOperations&version=1.0.0", map1, new NoHttpUtils.OnResponseListener() {
					@Override
					public void onSucceed(CommonClass commonClass, int i) {
						if (i == 0 && commonClass.getData() != null) {
							TestGetOpIdBean bean = JSON.parseObject(commonClass.getData().toString(), TestGetOpIdBean.class);
							if (bean != null && bean.getList() != null && bean.getList().size() > 0) {
								long opId = bean.getList().get(0).getCardUkid();
								Map<String, Object> map1 = new HashMap<>();
								map1.put("businessUnitId", sp.getValue(Constant.sp_User_Id));
								//有没有分类你自己心里没有一点逼数吗 templateType
								map1.put("templateType", getRandomString());
								map1.put("operationUkid", opId);
								NoHttpUtils.httpPost(ContractConstant.GET_IMPORT_TOOLS_INDEX, map1, new NoHttpUtils.OnResponseListener() {
									@Override
									public void onSucceed(CommonClass commonClass, int i) {
										if (commonClass != null && commonClass.getData() != null) {
											GetImportToolIndexBean bean = JSON.parseObject(commonClass.getData().toString(), GetImportToolIndexBean.class);
											if (bean != null) {
												if (bean.getPageType() == 1) {
													//跳转到生成短链接的页面
													Bundle bundle = new Bundle();
													bundle.putSerializable(ContractConstant.KEY_INTENT_IMPORT_TOOL_BEAN, bean);
													BulkImportFragment bulkImportFragment = new BulkImportFragment();
													bulkImportFragment.setArguments(bundle);
													pushFragment(bulkImportFragment);
												} else if (bean.getPageType() == 3) {
													//跳转到时间页面
													Bundle bundle = new Bundle();
													bundle.putSerializable(ContractConstant.KEY_INTENT_IMPORT_TOOL_BEAN, bean);
													DurationTimeFragment durationTimeFragment = new DurationTimeFragment();
													durationTimeFragment.setArguments(bundle);
													pushFragment(durationTimeFragment);
												}
											}
										}
									}

									@Override
									public void onFailed(String s, int i) {

									}

									@Override
									public void onFinish(int i) {

									}
								} , 0);


							}
						}
					}

					@Override
					public void onFailed(String s, int i) {

					}

					@Override
					public void onFinish(int i) {

					}
				} , 0);

			}
		});

		//任务卡片未开始的
		view.findViewById(R.id.renwuno).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Map<String, Object> map = new HashMap<>();
				map.put("opUserId", sp.getValue(Constant.sp_User_Id));
				map.put("operationType", "IMPORT_EXCEL");
				map.put("ownerId",  sp.getValue(Constant.sp_User_Id));
				map.put("status", 0);

				Map<String, Object> map1 = new HashMap<>();
				map1.put("pageNo", 1);
				map1.put("pageSize", 100);
				map1.put("params", map);

				NoHttpUtils.httpPost("router/api?method=TsOperationService.queryOperations&version=1.0.0", map1, new NoHttpUtils.OnResponseListener() {
					@Override
					public void onSucceed(CommonClass commonClass, int i) {
						if (i == 0 && commonClass.getData() != null) {
							TestGetOpIdBean bean = JSON.parseObject(commonClass.getData().toString(), TestGetOpIdBean.class);
							if (bean != null && bean.getList() != null && bean.getList().size() > 0) {
								long opId = bean.getList().get(0).getCardUkid();
								Map<String, Object> map1 = new HashMap<>();
								map1.put("businessUnitId", sp.getValue(Constant.sp_User_Id));
//								map1.put("templateType", getRandomString());
								map1.put("operationUkid", opId);
								NoHttpUtils.httpPost(ContractConstant.GET_IMPORT_TOOLS_INDEX, map1, new NoHttpUtils.OnResponseListener() {
									@Override
									public void onSucceed(CommonClass commonClass, int i) {
										if (commonClass != null && commonClass.getData() != null) {
											GetImportToolIndexBean bean = JSON.parseObject(commonClass.getData().toString(), GetImportToolIndexBean.class);
											if (bean != null) {
												if (bean.getPageType() == 1) {
													//跳转到生成短链接的页面
													Bundle bundle = new Bundle();
													bundle.putSerializable(ContractConstant.KEY_INTENT_IMPORT_TOOL_BEAN, bean);
													ImportHaveSortFragment bulkImportFragment = new ImportHaveSortFragment();
													bulkImportFragment.setArguments(bundle);
													pushFragment(bulkImportFragment);
												} else if (bean.getPageType() == 3) {
													//跳转到时间页面
													Bundle bundle = new Bundle();
													bundle.putSerializable(ContractConstant.KEY_INTENT_IMPORT_TOOL_BEAN, bean);
													DurationTimeFragment durationTimeFragment = new DurationTimeFragment();
													durationTimeFragment.setArguments(bundle);
													pushFragment(durationTimeFragment);
												}
											}
										}
									}

									@Override
									public void onFailed(String s, int i) {

									}

									@Override
									public void onFinish(int i) {

									}
								} , 0);


							}
						}
					}

					@Override
					public void onFailed(String s, int i) {

					}

					@Override
					public void onFinish(int i) {

					}
				} , 0);
			}
		});




	}
}
